package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Keson
 * @version 1.0
 * @description: 课程基本信息前端控制器
 * @date 2022/7/16 17:52
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * @description: 分页查询课程数据
     * @param: [request, dto]
     * @return: com.xuecheng.commons.model.vo.ResponseResult
     * @author Keson
     * @date: 2022/7/16 17:53
     */
    @PostMapping("list")
    public ResponseResult list(PageRequest request, @RequestBody CourseDto dto) {
        return courseBaseService.courseList(request, dto);
    }

    /**
     * 上传封面图片
     * 请求路径：POST /upload
     * 请求参数：
     * MultipartFile uploadFile
     * 返回值：
     * ResponseResult(图片地址)
     */
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile uploadFile) {
        return courseBaseService.upload(uploadFile);
    }

    /**
     * 保存课程
     * 请求路径： POST
     * 请求参数：
     * Body：CourseBaseDto
     * 响应数据courseBaseId
     */
    @PostMapping
    public ResponseResult saveCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.saveCourseBase(dto);
    }


    //根据id查询
    @GetMapping("/{courseBaseId}")
    public ResponseResult findById(@PathVariable("courseBaseId") String courseBaseId) {
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CourseBaseVo courseBaseVo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        return ResponseResult.okResult(courseBaseVo);
    }

    //更新课程
    @PutMapping
    public ResponseResult updateCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.updateCourseBase(dto);
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public ResponseResult deleteCourseBase(@PathVariable("courseId") String courseId) {
        return courseBaseService.deleteCourseBase(courseId);
    }

    //提交审核
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commit(@PathVariable("courseBaseId") Long courseBaseId) {
        return courseBaseService.commit(courseBaseId);
    }

    @PostMapping("/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto) {
        return courseBaseService.approve(dto);
    }
}
